/**********************************************************************
 * FILE : JAXBUtil.java
 * CREATE DATE : 2008-12-5
 * DESCRIPTION :
 *		JAXB的工具类用于将java对象转换成XML数据,或者反解析
 *      
 * CHANGE HISTORY LOG
 *---------------------------------------------------------------------
 * NO.|    DATE    |     NAME     |     REASON     | DESCRIPTION
 *---------------------------------------------------------------------
 * 1  | 2008-12-5 |  xiaoxiao  |    创建草稿版本
 *---------------------------------------------------------------------              
 **********************************************************************
 */
package com.efast.cafe.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * <p>
 * JAXB的工具类用于将java对象转换成XML数据,或者反解析。
 * 
 * @author xiaoxiao
 */
public class JAXBUtil {
	private final static Log log = LogFactory.getLog(JAXBUtil.class);
	public static QName defaultQName = new QName("BIZ_CONTENT");

	/**
	 * 把相应的java数据对象解析为相应的XML格式文件.
	 * 
	 * @param obj
	 *            java数据对象
	 * @return 解析后的信息
	 */
	public static String marshal(Object obj) {
		StringWriter sw = new StringWriter();
		JAXB.marshal(obj, sw);
		return sw.toString();
	}
	
	public static String marshalObject(Object obj) throws JAXBException{
			JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
			
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);	
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
			
			StringWriter stringWriter = new StringWriter();
//			JAXBElement<Object> jaxbElement = new JAXBElement<Object>(new QName("BIZ_CONTENT"), Object.class, obj);
//			jaxbMarshaller.marshal(jaxbElement, stringWriter);
	        jaxbMarshaller.marshal(obj, stringWriter);
	        return stringWriter.toString();
		
	}
	
	
	
	public static String marshalObject(Object obj, Class<?>[] classes) throws JAXBException {
		JAXBContext jaxbContext = null;
		if (classes != null && classes.length > 0)
			jaxbContext = JAXBContext.newInstance(classes);
		else
			jaxbContext = JAXBContext.newInstance(obj.getClass());
		
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);	
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
		
		StringWriter stringWriter = new StringWriter();
        jaxbMarshaller.marshal(obj, stringWriter);
        return stringWriter.toString();
	}
	
	

	/*public static String marshal(Class<?> classType, Object obj){
		return marshal(classType, obj, defaultQName);
	}
	public static String marshal(Class<?> classType, Object obj, String qName) {
		if (qName.equals("") || qName == null) {
			return marshal(classType, obj, defaultQName);
		} else
			return marshal(classType, obj, new QName(qName));
	}
	
	public static String marshal(Class<?> classType, Object obj, QName qName) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(classType);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);	
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
			StringWriter stringWriter = new StringWriter();
			JAXBElement jaxbElement = new JAXBElement(qName, classType, obj);
	        jaxbMarshaller.marshal(jaxbElement, stringWriter);
	        return stringWriter.getBuffer().toString();
		} catch (JAXBException e) {
			e.printStackTrace();
			return null;
		}
	}*/

	/**
	 * 把XML信息文件转换为给定类型的java对象，XML信息文本默认以"UTF-8"编码读取。
	 * 
	 * @param type
	 *            给定的java对象类。
	 * @param xmlcontent
	 *            信息内容
	 * @return 返回给定类型的java对象。
	 */
	public static <T> T unmarshal(String xmlcontent, Class<T> type) {
		return unmarshal(xmlcontent, "UTF-8", type);
	}

	/**
	 * 把XML信息文件转换为给定类型的java对象，XML信息文本默认以"UTF-8"编码读取。
	 * 
	 * @param type
	 *            给定的java对象类。
	 * @param contentEncoding
	 *            XML信息内容的编码。
	 * @param xmlcontent
	 *            信息内容
	 * @return 返回给定类型的java对象。
	 */
	public static <T> T unmarshal(String xmlcontent, String contentEncoding,
			Class<T> type) {
		if (xmlcontent == null || "".equals(xmlcontent)) {
			return null;
		}
		if (contentEncoding == null || "".equals(contentEncoding.trim())) {
			contentEncoding = "UTF-8";
		}
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(xmlcontent.getBytes(contentEncoding));
			return JAXB.unmarshal(is, type);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					log.error(e);
				}
				is = null;
			}
		}
	}
}
